package com.awei.study.design.structure.flyWeight;

import java.util.HashMap;

/**
 * @Author liweilong
 * @Date 2024/8/9 15:25
 * 享元模式
 */
public class FlyWeight {

    public static abstract class AbstractBox {
        public abstract String getShape();

        public void display(String color) {
            System.out.println("形状为：" + getShape() + "，颜色为：" + color);
        }
    }

    public static class IBox extends AbstractBox {

        @Override
        public String getShape() {
            return "I";
        }
    }

    public static class LBox extends AbstractBox {

        @Override
        public String getShape() {
            return "L";
        }
    }

    public static class OBox extends AbstractBox {

        @Override
        public String getShape() {
            return "O";
        }
    }

    public static class BoxFactory {
        private static HashMap<String,AbstractBox> map;

        private BoxFactory() {
            map = new HashMap<>();
            AbstractBox ibox = new IBox();
            AbstractBox lBox = new LBox();
            AbstractBox oBox = new OBox();
            map.put("i",ibox);
            map.put("l",lBox);
            map.put("o",oBox);
        }

        public static final BoxFactory getInstance() {
            return SingletonHolder.INSTANCE;
        }

        private static class SingletonHolder {
            private static final BoxFactory INSTANCE = new BoxFactory();
        }
    }
}
